package com.test.daily.leetcode.y2022.m05.day0525.v01;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @descriptions: Solution
 * @author: Tom
 * @date: 2022/5/25 下午 04:09
 * @version: 1.0
 */
public class Solution {
    // K 代表可以做多少投资
    // W 代表启动资金
    // Profits 每个项目的收益数组
    // Capital 每个项目的投资大小，花费的钱
    public static int maxProfile(int K, int W, int[] Profits, int[] Capital){
        PriorityQueue<Program> pMax = new PriorityQueue<>(new MaxProfitComparator());
        PriorityQueue<Program> cMin = new PriorityQueue<>(new MinCostComparator());
        Program[] pros = new Program[Profits.length];
        for (int i = 0; i < pros.length; i++) {
            pros[i] = new Program(Profits[i], Capital[i]);
            cMin.add(pros[i]);
        }
        for (int i = 0; i < K; i++) {
            while(cMin.peek().c < W){
                pMax.add(cMin.poll());
            }
            if(cMin.size() == 0){
                return W;
            }
            W += pMax.poll().p;
            return W;
        }

        return 0;
    }
    public static class MinCostComparator implements Comparator<Program> {

        @Override
        public int compare(Program o1, Program o2) {
            return o1.c - o2.c;
        }
    }

    public static class MaxProfitComparator implements Comparator<Program> {

        @Override
        public int compare(Program o1, Program o2) {
            return o2.p - o1.p;
        }
    }

    public static class Program {
        public int p;
        public int c;

        public Program(int p, int c) {
            this.p = p;
            this.c = c;
        }
    }
}
